package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("/login")
    public R<Employee> getById(HttpServletRequest request, @RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex(employee.getPassword().getBytes()));
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(null!=employee.getUsername(),Employee::getUsername,employee.getUsername());
        lqw.eq(null!=employee.getPassword(),Employee::getPassword,employee.getPassword());
        Employee one = employeeService.getOne(lqw);
        if(null!=one){
            if(one.getStatus()==0){
                return R.error("账号已禁用");
            }
            request.getSession().setAttribute("employee",one.getId());
            R<Employee> success = R.success(one);
            return success;
        }else {
            R<Employee> error = R.error("没有查到该用户，请核对用户名或密码");
            return error;
        }
    }
    @PostMapping("/logout")
    public R<Employee> logout(HttpServletRequest request){
//        System.out.println(1/0);
        request.getSession().removeAttribute("employee");
        R<Employee> ok = R.success(new Employee());
        return ok;
    }
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
//        employee.setUpdateTime(LocalDateTime.now());
        System.out.println(employee);
        boolean save = employeeService.save(employee);
        return R.success("添加成功了456798");

    }
    @GetMapping("/page")
    public R<IPage<Employee>> getPage(@RequestParam("page") Integer page,
                                     @RequestParam("pageSize") Integer pageSize,
                                       String name){
        System.out.println(page+"----------"+pageSize);
        IPage page1=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.like((name!=null &&name!=""),Employee::getName,name);
        IPage page2 =employeeService.page(page1,lqw);
        System.out.println(page2.getCurrent());
        System.out.println(page2.getSize());
        System.out.println(page2.getPages());
        System.out.println(page2.getTotal());
        System.out.println(page2.getRecords());
        return R.success(page2);

    }
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        Long id= (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(id);
        System.out.println(employee);
        boolean b = employeeService.updateById(employee);
        if (b){
            return R.success("状态修改成功");
        }else{
            return R.error("状态修改失败");
        }
    }
    @GetMapping("/{id}")
    public R<Employee> getId(@PathVariable Long id){
        LambdaQueryWrapper<Employee> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Employee::getId,id);
        Employee one = employeeService.getOne(lqw);
        return R.success(one);
    }

}
